<?php
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require 'utf8.php';
require 'database.php';
$db = new DB();

$getNow = new DateTime('now');
$today = $getNow->format('Y-m-d');

$cat = isset($_GET['c']) ? strval($_GET['c']) : NULL;
$cat = str_replace(' ', '+', $cat);
$p = isset($_GET['p']) ? strval($_GET['p']) : 1;

$page = '';
if ($p)
	$page = '&p=' . $p;

$url = 'http://www.ebog.com/load_page.php?l=en&u=unity-3d-' . $cat . $page;

crawl($url, $cat);

function crawl($url, $cat = '') {
	global $db, $today, $p;
	$array = array();

	$html = file_get_html($url);

	$link = '';
	foreach ($html->find('div.link') as $element){
		$link .= $element->innertext;
	}

	$links = str_get_html($link);

	foreach ($links->find('a') as $element){
		$plaintext = trim($element->plaintext);
		$plaintext = str_replace("'", '', $plaintext);
		$link = trim($element->href);

		$array['name'][] = $plaintext;
		$array['url'][] = remove_unicode($plaintext, true);
		$array['href'][] = $link;
	}

	// Images
	$images = '';
	foreach ($html->find('img.lazy') as $element){
		$images .= $element;
	}

	$images = str_replace('src', 'data-img', $images);
	$images = str_replace('data-original', 'src', $images);

	$_images = str_get_html($images);
	foreach ($_images->find('img') as $element){
		$img = trim($element->src);
		$array['image'][] = $img;
	}

// 	echo '<pre>';
// 	print_r($array);
// 	echo '</pre>';

	$total = count($array['url']);

	$num = 0;
	for ($i = 0; $i < $total; $i++) {
		// check url exists
		$Qry = 'SELECT crawl_url
				FROM games
				WHERE crawl_url = "' . $array['href'][$i] . '"';

		if ($db->numRows($Qry) == 0) {
			++$num;

			$sth = array('-games', 'games-');
			$cat = str_replace($sth, '', $cat);

			$data = array(
					'name'=>$array['name'][$i],
					'name_seo'=>$array['url'][$i],
					'image'=>$array['image'][$i],
					'tags'=>trim($cat),
					'date'=>$today,
					'crawl_url'=>$array['href'][$i],
					'source'=>'ebog.com'
			);

			$newID = $db->insert('games', $data);

			if ($newID) {
				$des = array(
						'id'=>$newID
				);
				$db->insert('games_description', $des);
			}

// 			echo '<pre>';
// 			print_r($data);
// 			echo '</pre>';
		}
	}

	echo 'Total games added: ' . $num . ' on page ' . $p;
	return $num;
}